/*
===============================================================================
The MIT License

Copyright (c) 2009 Brian Suojanen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
*/
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
//-----------------------------------------------------------------------------
#include "Exception.h"
#include "Playlist.h"
#include "MainFrame.h"
#include "wx/progdlg.h"
#include "wx/filename.h"
//-----------------------------------------------------------------------------
MainFrame::MainFrame() : MainFrameUI(NULL)
{
	// disable the export button
	this->exportButton->Disable();

	this->playlist = new Playlist();
}
//-----------------------------------------------------------------------------
MainFrame::~MainFrame()
{
	delete this->playlist;
}
//-----------------------------------------------------------------------------
void MainFrame::OnExit(wxCommandEvent& event)
{
	this->Close();
}
//-----------------------------------------------------------------------------
void MainFrame::OnPlaylistSelected(wxFileDirPickerEvent& event)
{
	// set the tooltip equal to the absolute file path
	this->playlistPicker->SetToolTip(this->playlistPicker->GetPath());

	// extract the file names from the playlist file...
	wxArrayString files;

	try
	{
		files = this->playlist->GetFiles(this->playlistPicker->GetPath());
	}
	catch(Exception& e)
	{
		wxLogError(_T("%s"), e.GetMessage().c_str());
	}

	this->filesListBox->Set(files);

	if(files.Count() < 1)
	{
		wxLogMessage(_T("%s"), _T("There are no songs in this playlist."));
	}

	// can we enable the export button?
	if(this->folderPicker->GetPath().IsEmpty())
	{
		// no
		this->exportButton->Disable();
	}
	else
	{
		// maybe
		if(this->filesListBox->GetCount() > 0)
		{
			// yes, we have files
			this->exportButton->Enable();
		}
		else
		{
			// no, no files
			this->exportButton->Disable();
		}
	}
}
//-----------------------------------------------------------------------------
void MainFrame::OnFolderSelected(wxFileDirPickerEvent& event)
{
	this->folderPicker->SetToolTip(this->folderPicker->GetPath());

	// can we enable the export button?
	if(this->playlistPicker->GetPath().IsEmpty())
	{
		// no
		this->exportButton->Disable();
	}
	else
	{
		// maybe
		if(this->filesListBox->GetCount() > 0)
		{
			// yes, we have files
			this->exportButton->Enable();
		}
		else
		{
			// no, no files
			this->exportButton->Disable();
		}
	}
}
//-----------------------------------------------------------------------------
void MainFrame::OnExport(wxCommandEvent& event)
{
	// grab the file names
	wxArrayString files = this->filesListBox->GetStrings();
	// the progress dialog needs to know how many files
	int max = files.Count();

	// fire up a progress dialog...
	wxProgressDialog dialog(wxT("Export Progress"), wxT("Exporting to ") + this->folderPicker->GetPath(), max, this, wxPD_CAN_ABORT | wxPD_APP_MODAL | wxPD_ELAPSED_TIME | wxPD_ESTIMATED_TIME | wxPD_REMAINING_TIME);
	// the user can cancel the export
	bool cont = true;

	for (int i = 0; i < max; i++)
	{
		// grab a file name
		wxString file = files.Item(i);
		// update the progress (but has the user clicked Cancel?)
		cont = dialog.Update(i, wxT("Copying: " + file));

		if (! cont)
		{
			if (wxMessageBox(wxT("Do you want to cancel the playlist export?"), wxT("Cancel export"), wxYES_NO | wxICON_QUESTION) == wxYES)
			{
				// user wants to abort the export
				break;
			}
			dialog.Resume();
		}

		// check source file exists
		if (wxFileName::FileExists(file))
		{
			// Get the absolute file path for the destination...
			wxString file2 = this->GetDestinationFile(file, this->folderPicker->GetPath(), i);
			// and copy it to the destination folder
			::wxCopyFile(file, file2, true);
		}
		else
		{
			wxString msg = _T("The file, ") + file + _T(", cannot be located.");
			wxLogWarning(_T("%s"), msg.c_str());
		}
	}

	// reset the UI
	this->playlistPicker->GetTextCtrl()->Clear();
	this->playlistPicker->SetToolTip(wxT("Browse..."));
	this->filesListBox->Clear();
	this->folderPicker->GetTextCtrl()->Clear();
	this->folderPicker->SetToolTip(wxT("Browse..."));
	this->exportButton->Disable();
}
//-----------------------------------------------------------------------------
wxString MainFrame::GetDestinationFile(wxString sourceFile, wxString destinationFolder, int trackNumber)
{
	wxFileName filenamePath(sourceFile);
	wxString filename = filenamePath.GetFullName();

	// turn the track number into a string...
	wxString prefix = wxString::Format(wxT("%d"), (int)trackNumber);

	// if track number is LT 10, prepend a zero
	if(trackNumber < 10)
	{
		prefix = wxT("0") + prefix;
	}
	filename = prefix + wxT(" - ") + filename;

	wxString destFilepath(destinationFolder);
	destFilepath += wxFileName::GetPathSeparator(wxPATH_NATIVE);
	destFilepath += filename;
	return destFilepath;
}
//-----------------------------------------------------------------------------
